import {
    ClassicModel
} from '../../models/ClassicModel'
import {
    LikeModel
} from '../../models/LikeModel'

const classicModel = new ClassicModel()
const likeModel = new LikeModel()
// pages/classic/classic.js

Component({

    /**
     * 页面的初始数据
     */
    properties: {
        cid: Number,
        type: Number
    },

    data: {
        classic: null,
        likeStatus: true,
        likeNumber: 0,
        first: false,
        latest: true

    },

    /**
     * 生命周期函数--监听页面加载
     */
    attached (option) {
        const cid = this.properties.cid
        const type = this.properties.type
        if (!cid) {
            classicModel.getLatest((res) => {
                this.setData({
                    classic: res,
                    likeStatus: res.like_status,
                    likeNumber: res.fav_nums
                })
            })
        } else {
            classicModel.getById(cid, type, res => {
                this._getLikeStatus(res.id, res.type)
                this.setData({
                    classic: res,
                    latest: classicModel.isLatest(res.index),
                    first: classicModel.isFirst(res.index)
                })
            })
        }
    },

    methods: {

        onLike (event) {
            const {
                behavior
            } = event.detail
            likeModel.like(behavior, this.data.classic.id, this.data.classic.type)
        },

        onPrevious () {
            this._updateClassic('previous')
        },

        onNext () {
            this._updateClassic('next')
        },

        _updateClassic (nextOrPrevious) {
            const {
                index
            } = this.data.classic
            classicModel.getClassic(index, nextOrPrevious, (res) => {
                this._getLikeStatus(res.id, res.type)
                this.setData({
                    classic: res,
                    latest: classicModel.isLatest(res.index),
                    first: classicModel.isFirst(res.index)
                })
            })
        },

        _getLikeStatus (artID, category) {
            likeModel.getClassLikeStatus(artID, category, (res) => {
                this.setData({
                    likeStatus: res.like_status,
                    likeNumber: res.fav_nums
                })
            })
        }

        // onShareAppMessage: function (res) {
        //     console.log(res)
        //     return {
        //         title: '自定义转发标题',
        //         path: '/page/user?id=123',
        //         imageUrl: 'http://img12.3lian.com/gaoqing02/01/58/85.jpg'
        //     }
        // }
    }

})
